package com.zhss.eshop.common.json;

import com.alibaba.fastjson.JSONObject;

/**
 * json字段值提取语法的解析器
 * @author suyuan
 *
 */
public class JsonExtractSyntaxParser {

	private static final String JSON_EXTRACT_SYNTAX_SPLITTER = "/";

	/**
	 * 解析json字段值提取语法
	 * @param jsonExtractSyntax json字段值提取语法
	 * @return 抽象语法树
	 * @throws Exception
	 */
	public static JsonExpression parse(String jsonExtractSyntax) throws Exception {
		JsonExpression rootJsonExpression = null;
		CompositeJsonExpression currentJsonExpression = null;

		String[] elements = jsonExtractSyntax.split(JSON_EXTRACT_SYNTAX_SPLITTER);

		for(int i = 0; i < elements.length; i++) {
			if(i == 0) {
				if(elements.length == 1) {
					rootJsonExpression = new LeafJsonExpression(elements[i]);
				} else {
					rootJsonExpression = new CompositeJsonExpression(elements[i]);
					currentJsonExpression = (CompositeJsonExpression) rootJsonExpression;
				}
			} else if(i < elements.length - 1) {
				CompositeJsonExpression childJsonExpression = new CompositeJsonExpression(elements[i]);
				currentJsonExpression.setChildJsonExpression(childJsonExpression);
				currentJsonExpression = childJsonExpression;
			} else {
				LeafJsonExpression childJsonExpression = new LeafJsonExpression(elements[i]);
				currentJsonExpression.setChildJsonExpression(childJsonExpression);
			}
		}

		return rootJsonExpression;
	}

	public static void main(String[] args) throws Exception {
		String s="{" +
				"\"a\": {" +
				"\"a1\": {" +
				"\"thresholdAmount\": 300.0" +
				"}" +
				"}," +
				"\"b\": {" +
				"\"b1\": {" +
				"\"reduceAmount\": 100.0" +
				"}" +
				"}" +
				"}";
		JsonExtractor jsonExtractor=new JsonExtractor();
		JSONObject rule = JSONObject.parseObject(s);
		Double discountAmount = jsonExtractor.getDouble(rule, "b/b1/reduceAmount");
		System.out.println(discountAmount);
	}

}
